﻿using Microsoft.EntityFrameworkCore;
using WhaleAccount.Models;
using Type = WhaleAccount.Models.Type;

namespace WhaleAccount.Helpers;
public class DatabaseContext : DbContext
{
    private static string? _dbpath;
    private static DatabaseContext? _dbContext;
    public DbSet<Record> Records
    {
        get; set;
    }
    public DbSet<Type> Types
    {
        get; set;
    }

    private DatabaseContext()
    {

    }

    public static void Init(string dbpath)
    {
        _dbpath = dbpath;
        _dbContext ??= new DatabaseContext();

        var notExisted = _dbContext.Database.EnsureCreated();
        if (notExisted)
        {
            _dbContext.Types.Add(new Type(1, "餐饮", 0, "🍽", 1));
            _dbContext.Types.Add(new Type(2, "购物", 0, "🛍", 2));
            _dbContext.Types.Add(new Type(3, "日用", 0, "🧻", 3));
            _dbContext.Types.Add(new Type(4, "交通", 0, "🚌", 4));
            _dbContext.Types.Add(new Type(5, "水果", 0, "🍒", 5));
            _dbContext.Types.Add(new Type(6, "零食", 0, "🍦", 6));
            _dbContext.Types.Add(new Type(7, "运动", 0, "🏃", 7));
            _dbContext.Types.Add(new Type(8, "娱乐", 0, "🎤", 8));
            _dbContext.Types.Add(new Type(9, "通讯", 0, "☎", 9));
            _dbContext.Types.Add(new Type(10, "服饰", 0, "👕", 10));
            _dbContext.Types.Add(new Type(11, "医疗", 0, "💊", 11));
            _dbContext.Types.Add(new Type(12, "书籍", 0, "📓", 12));
            _dbContext.Types.Add(new Type(13, "游戏", 0, "🎮", 13));
            _dbContext.Types.Add(new Type(14, "住房", 0, "🏠", 14));
            _dbContext.Types.Add(new Type(15, "数码", 0, "🖥", 15));
            _dbContext.Types.Add(new Type(16, "礼物", 0, "🎁", 16));
            _dbContext.Types.Add(new Type(17, "理发", 0, "💇", 17));

            _dbContext.Types.Add(new Type(18, "工资", 1, "💳", 18));
            _dbContext.Types.Add(new Type(19, "理财", 1, "💹", 19));
            _dbContext.Types.Add(new Type(20, "礼金", 1, "🧧", 20));
            _dbContext.Types.Add(new Type(21, "其它", 1, "💰", 21));

            _dbContext.SaveChanges();
        }
    }

    public static DatabaseContext? GetDbContext() => _dbContext;

    protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite($"Data Source={_dbpath}");
}
